import userApi from "@/api/system-settings/user-manage";
import maintenanceTaskTemplateApi from "@/api/equip-maintenance-manage/maintenance-task-template";
import maintenanceTaskApi from "@/api/equip-maintenance-manage/maintenance-task";
import maintenanceDataitemApi from "@/api/equip-maintenance-manage/maintenance-dataitem";

import inspectionTaskTemplateApi from "@/api/equip-inspection/inspection-task-template";
import inspectionDataitemApi from "@/api/equip-inspection/inspection-dataitem";
import inspectionTaskApi from "@/api/equip-inspection/inspection-task";

import equipScrapRecordApi from "@/api/equip-manage/equip-scrap-record";
export const maintenanceMixins = {
  data() {
    return {
      isShowEquip: false,
      //任务管理设备
      showCheckList: [],
      //保养人
      mixUserList: [],
      mixGenerateNumList: [
        { generateNumId: 0, generateNumName: "不重复执行" },
        { generateNumId: 1, generateNumName: "一天一次" },
        { generateNumId: 2, generateNumName: "三天一次" },
        { generateNumId: 3, generateNumName: "七天一次" },
        { generateNumId: 4, generateNumName: "十五天一次" },
        { generateNumId: 5, generateNumName: "三十天一次" },
      ],
      mixGenerateHourList: [
        { generateHourId: 0, generateHourName: "00:00:00" },
        { generateHourId: 1, generateHourName: "01:00:00" },
        { generateHourId: 2, generateHourName: "02:00:00" },
        { generateHourId: 3, generateHourName: "03:00:00" },
        { generateHourId: 4, generateHourName: "04:00:00" },
        { generateHourId: 5, generateHourName: "05:00:00" },
        { generateHourId: 6, generateHourName: "06:00:00" },
        { generateHourId: 7, generateHourName: "07:00:00" },
        { generateHourId: 8, generateHourName: "08:00:00" },
        { generateHourId: 9, generateHourName: "09:00:00" },
        { generateHourId: 10, generateHourName: "10:00:00" },
        { generateHourId: 11, generateHourName: "11:00:00" },
        { generateHourId: 12, generateHourName: "12:00:00" },
        { generateHourId: 13, generateHourName: "13:00:00" },
        { generateHourId: 14, generateHourName: "14:00:00" },
        { generateHourId: 15, generateHourName: "15:00:00" },
        { generateHourId: 16, generateHourName: "16:00:00" },
        { generateHourId: 17, generateHourName: "17:00:00" },
        { generateHourId: 18, generateHourName: "18:00:00" },
        { generateHourId: 19, generateHourName: "19:00:00" },
        { generateHourId: 20, generateHourName: "20:00:00" },
        { generateHourId: 21, generateHourName: "21:00:00" },
        { generateHourId: 22, generateHourName: "22:00:00" },
        { generateHourId: 23, generateHourName: "23:00:00" },
      ],
      performTimeList: [
        { performTimeName: "第一天00:00:00", performTimeKey: 0 },
        { performTimeName: "第一天01:00:00", performTimeKey: 1 },
        { performTimeName: "第一天02:00:00", performTimeKey: 2 },
        { performTimeName: "第一天03:00:00", performTimeKey: 3 },
        { performTimeName: "第一天04:00:00", performTimeKey: 4 },
        { performTimeName: "第一天05:00:00", performTimeKey: 5 },
        { performTimeName: "第一天06:00:00", performTimeKey: 6 },
        { performTimeName: "第一天07:00:00", performTimeKey: 7 },
        { performTimeName: "第一天08:00:00", performTimeKey: 8 },
        { performTimeName: "第一天09:00:00", performTimeKey: 9 },
        { performTimeName: "第一天10:00:00", performTimeKey: 10 },
        { performTimeName: "第一天11:00:00", performTimeKey: 11 },
        { performTimeName: "第一天12:00:00", performTimeKey: 12 },
        { performTimeName: "第一天13:00:00", performTimeKey: 13 },
        { performTimeName: "第一天14:00:00", performTimeKey: 14 },
        { performTimeName: "第一天15:00:00", performTimeKey: 15 },
        { performTimeName: "第一天16:00:00", performTimeKey: 16 },
        { performTimeName: "第一天17:00:00", performTimeKey: 17 },
        { performTimeName: "第一天18:00:00", performTimeKey: 18 },
        { performTimeName: "第一天19:00:00", performTimeKey: 19 },
        { performTimeName: "第一天20:00:00", performTimeKey: 20 },
        { performTimeName: "第一天21:00:00", performTimeKey: 21 },
        { performTimeName: "第一天22:00:00", performTimeKey: 22 },
        { performTimeName: "第一天23:00:00", performTimeKey: 23 },
        { performTimeName: "第二天00:00:00", performTimeKey: 24 },
        { performTimeName: "第二天01:00:00", performTimeKey: 25 },
        { performTimeName: "第二天02:00:00", performTimeKey: 26 },
        { performTimeName: "第二天03:00:00", performTimeKey: 27 },
        { performTimeName: "第二天04:00:00", performTimeKey: 28 },
        { performTimeName: "第二天05:00:00", performTimeKey: 29 },
        { performTimeName: "第二天06:00:00", performTimeKey: 30 },
        { performTimeName: "第二天07:00:00", performTimeKey: 31 },
        { performTimeName: "第二天08:00:00", performTimeKey: 32 },
        { performTimeName: "第二天09:00:00", performTimeKey: 33 },
        { performTimeName: "第二天10:00:00", performTimeKey: 34 },
        { performTimeName: "第二天11:00:00", performTimeKey: 35 },
        { performTimeName: "第二天12:00:00", performTimeKey: 36 },
        { performTimeName: "第二天13:00:00", performTimeKey: 37 },
        { performTimeName: "第二天14:00:00", performTimeKey: 38 },
        { performTimeName: "第二天15:00:00", performTimeKey: 39 },
        { performTimeName: "第二天16:00:00", performTimeKey: 40 },
        { performTimeName: "第二天17:00:00", performTimeKey: 41 },
        { performTimeName: "第二天18:00:00", performTimeKey: 42 },
        { performTimeName: "第二天19:00:00", performTimeKey: 43 },
        { performTimeName: "第二天20:00:00", performTimeKey: 44 },
        { performTimeName: "第二天21:00:00", performTimeKey: 45 },
        { performTimeName: "第二天22:00:00", performTimeKey: 46 },
        { performTimeName: "第二天23:00:00", performTimeKey: 47 },
      ],
      //所有未作废设备
      mixAllEquipmentList: [],
      dataItemList: []
    }
  },
  created() {
  },
  methods: {
    clearValidate() {
      this.formData = {};
      this.$refs.ruleForm.clearValidate();
      this.$refs["ruleForm"].resetFields();
    },
    // 转换时间
    mixChangeGenerateHour(item) {
      let data = "";
      console.log("mixChangeGenerateHour", item);
      var index = this.mixGenerateHourList.findIndex(
        (a) => a.generateHourId === item
      );
      if (index !== -1) {
        data = this.mixGenerateHourList[index]["generateHourName"];
      }
      return data;
    },
    // 转换时间
    mixChangePerformTime(row) {
      let start = "";
      let end = "";
      this.performTimeList.forEach((a) => {
        if (a.performTimeKey === row.performStartTime) {
          start = a["performTimeName"] + "~";
        } else if (a.performTimeKey === row.performEndTime) {
          end = a["performTimeName"];
        }
      });
      return start + end;
    },
    // 所有设备
    mixQueryEquipment() {
      this.mixAllEquipmentList = [];
      let params = {};
      equipScrapRecordApi.queryEquipment(params).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          for (var i in data) {
            this.mixAllEquipmentList.push({
              equipmentName: data[i],
              equipmentCode: i,
            });
          }
        }
      });
    },
    // 查询可选择保养人
    mixQueryUserList() {
      userApi.queryUserAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.mixUserList = data;
        }
      });
    },
    /***-------------保养管理模块---------------->***/
    //  任务模版  查询已绑定的检查项和设备
    mixQueryMaintenancePlanTaskModel(planTaskModelId) {
      console.log("mixQueryMaintenancePlanTaskModel");
      var params = {
        planTaskModelId,
      };
      this.showCheckList = [];
      maintenanceTaskTemplateApi.query(params).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          let getPlanTaskModelList = data["planTaskEquipmentMapperList"] || [];
          this.getEquipmentAndDataItem(getPlanTaskModelList, 'planTaskCheckItemMapperList')
        }
        this.isShowEquip = true;
      });
    },
    //任务  查询已绑定的检查项和设备
    mixQueryMaintenancePlanTask(taskId) {
      console.log("mixQueryMaintenancePlanTask");
      var params = {
        taskId,
      };
      this.showCheckList = [];
      maintenanceTaskApi.query(params).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          let getPlanTaskList = data["taskEquipmentMapperList"] || [];
          this.getEquipmentAndDataItem(getPlanTaskList, 'taskCheckItemMapperList')
          this.isShowEquip = true;
        }
      });
    },

    // 查询该设备下的点检项
    mixQueryMaintenanceHaveCheckList() {
      maintenanceDataitemApi.queryConfigList().then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.dataItemList = data;
        }
      });
    },
    /*<--------------------*/



    /*------------------点检管理模块--------------->*/
    //任务模版  查询已绑定的检查项和设备
    mixQueryInspectionPlanTaskModel(planTaskModelId) {
      console.log("mixQueryPlanTaskModel");
      var params = {
        planTaskModelId,
      };
      this.showCheckList = [];
      inspectionTaskTemplateApi.query(params).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          let getPlanTaskModelList = data["planTaskEquipmentMapperList"] || [];
          this.getEquipmentAndDataItem(getPlanTaskModelList, 'planTaskCheckItemMapperList')
          this.isShowEquip = true;
        }
      });
    },
    //任务  查询已绑定的检查项和设备
    mixQueryInspectionPlanTask(taskId) {
      console.log("mixQueryPlanTask");
      var params = {
        taskId,
      };
      this.showCheckList = [];
      inspectionTaskApi.query(params).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          let getPlanTaskList = data["taskEquipmentMapperList"] || [];
          this.getEquipmentAndDataItem(getPlanTaskList, 'taskCheckItemMapperList')
          this.isShowEquip = true;
        }
      });
    },

    // 查询该设备下的点检项
    mixQueryInspectionHaveCheckList() {
      inspectionDataitemApi.queryConfigList().then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.dataItemList = data;
        }
      });
    },

    /*<--------------------------------------*/

    getEquipmentAndDataItem(getPlanTaskModelList, CheckItemMapperListName) {
      if (getPlanTaskModelList.length > 0) {
        for (var i in getPlanTaskModelList) {
          let equipmentCode = getPlanTaskModelList[i]["equipmentCode"];
          // 收集已选点检项
          var src = {
            equipmentCode,
            equipmentName: getPlanTaskModelList[i]["equipmentName"],
          };
          src["checkItemContentList"] = [];
          //遍历模版设置的数据检查项
          if (
            getPlanTaskModelList[i][CheckItemMapperListName].length >
            0
          ) {
            for (var k in getPlanTaskModelList[i][
              CheckItemMapperListName
            ]) {
              //数据检查项ID
              let equipmentCheckItemMapperId =
                getPlanTaskModelList[i][CheckItemMapperListName][k][
                "equipmentCheckItemMapperId"
                ];
              //遍历并比较所有设备检查数据项的设备ID和数据项ID，来获得选择的检查项
              for (let n = 0; n < this.dataItemList.length; n++) {
                let dataItem = this.dataItemList[n];
                if (
                  dataItem.equipmentCode === equipmentCode &&
                  dataItem.equipmentCheckItemMapperId ===
                  equipmentCheckItemMapperId
                ) {
                  src["checkItemContentList"].push(dataItem);
                  break;
                }
              }
            }
          }
          console.log("getEquipmentAndDataItem--src", src);
          this.showCheckList.push(src);
        }
      }
      //console.log("getEquipmentAndDataItem--data", this.showCheckList);
    },


  }
}
